/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans.Requests.DataRetrieval;

import Beans.Requests.RequestBean;
import Utilities.Constants.ResourceIdType;
import Utilities.Constants.ResourceRequestType;

/**
 * This is the superclass for all data retrieval requests.
 *
 * @author Angel Sanadinov
 */
public abstract class DataRetrievalRequest extends RequestBean
{
    private ResourceRequestType resourceRequestType; //type of request (single/multiple)

    /**
     * No-argument constructor. <br><br>
     * All fields are set to their invalid values.
     *
     * @see ResourceRequestType#INVALID_RESOURCE
     */
    public DataRetrievalRequest()
    {
        super();
        this.resourceRequestType = ResourceRequestType.INVALID_RESOURCE;
    }

    /**
     * Constructs the object using the specified user and server IDs. <br><br>
     *
     * When the required resource is specific, <code>resourceRequestType</code>
     * should be set to <code>SINGLE_RESOURCES</code>; when a list of all available
     * resources is required, <code>resourceRequestType</code> should be set to
     * <code>MULTIPLE_RESOURCES</code>;
     *
     * @param requestorId the id of the user that made the request
     * @param serverId the server on which the requested action is to take place
     * @param resourceRequestType the type of required resources
     */
    public DataRetrievalRequest(int requestorId, int serverId, ResourceRequestType resourceRequestType)
    {
        setRequestorId(requestorId);
        setServerId(serverId);
        this.resourceRequestType = resourceRequestType;
    }

    /**
     * Retrieves the resource request type.
     *
     * @return the resource request type
     */
    public ResourceRequestType getResourceRequestType()
    {
        return resourceRequestType;
    }

    /**
     * Sets the resource request type.
     *
     * @param resourceRequestType the resource request type
     */
    public void setResourceRequestType(ResourceRequestType resourceRequestType)
    {
        this.resourceRequestType = resourceRequestType;
    }

    /**
     * Checks if the resource id associated with this object is valid.
     *
     * @return <code>true</code> if the id is valid or <code>false</code> otherwise
     */
    protected abstract boolean isResourceIdValid();

    /**
     * Retrieves the type of the resource id.
     *
     * @return the resource id type
     */
    public abstract ResourceIdType getResourceIdType();

    /**
     * Checks the validity of the request object. <br><br>
     *
     * The object is considered valid only if all supplied data is valid.
     *
     * @return <code>true</code> if the object is valid or <code>false</code> otherwise
     */
    @Override
    public boolean isValid()
    {
        if(isRequestedActionValid() && isRequestorIdValid() && isServerIdValid()
                && resourceRequestType != null && resourceRequestType != ResourceRequestType.INVALID_RESOURCE
                && (resourceRequestType == ResourceRequestType.MULTIPLE_RESOURCES || isResourceIdValid()))
            return true;
        else
            return false;
    }
}
